package com.skyjing.rvdadapter

import android.content.Context
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.databinding.ViewDataBinding


abstract class CommonAdapter<T, V : ViewDataBinding>(
    protected var mContext: Context,
    protected var mLayoutId: Int,
    protected var mDatas: List<T>
) : RecyclerView.Adapter<ViewHolder<V>>() {

    val NO_DATA = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder<V> {
        val v = DataBindingUtil.inflate<V>(LayoutInflater.from(mContext), mLayoutId, parent, false)
        return ViewHolder(v)
    }


    override fun onBindViewHolder(holder: ViewHolder<V>, position: Int) {
        //        holder.updatePosition(position);
        convert(holder.t, mDatas[position], position)
    }

    abstract fun convert(v: V, t: T, position: Int)

    override fun getItemCount(): Int {
        return if (!mDatas.isNullOrEmpty()) mDatas.size else 0
    }

}